package ren.kane.asdppp.activeobject;

/**
 * @author renrk(1275) on 2019-04-22 3:03 PM
 **/
public class SleepCommand implements Command {

    private Command wakeupCommand = null;

    private ActiveObjectEngine activeObjectEngine = null;

    private long sleepTime = 0;

    private long startTime = 0;

    private boolean started = false;

    public SleepCommand() {
    }

    public SleepCommand(long milliseconds, ActiveObjectEngine activeObjectEngine, Command wakeupCommand) {
        sleepTime = milliseconds;
        this.activeObjectEngine = activeObjectEngine;
        this.wakeupCommand = wakeupCommand;
    }

    @Override
    public void execute() throws Exception {
        long currentTime = System.currentTimeMillis();
        if (!started) {
            started = true;
            startTime = currentTime;
            activeObjectEngine.addCommand(this);
        } else if ((currentTime - startTime) < sleepTime) {
            activeObjectEngine.addCommand(this);
        } else {
            activeObjectEngine.addCommand(wakeupCommand);
        }
    }
}
